package top.wardyan.wdev.dbdevassist.model;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import top.wardyan.base.util.DateUtil;

@SuppressWarnings("serial")
public class YTUser extends MasterModel<YTUser>{

	public static final YTUser dao = new YTUser().dao();
	public static final YTUser NULL = new YTUser().setSID(0).setXName("").setXNote("")
			.setSCreateTime(DateUtil.getCurrentTimestamp())
			.setSLastUpdate(DateUtil.getCurrentTimestamp())
			.setSStatus(1).setSVersion(0).setXAccount("")
			.setXIP("").setXOption(0).setXPassword("").setXPCName("");
	@Override
	public String tableName() {
		return "yt_user";
	}
	

	
	
	
	
	private static Timestamp deadline = null;
	private static Map<Integer, YTUser> list = new HashMap<>();
	
	public static YTUser getInstance(int sID) {
		return getInstance(sID, true);
	}
	
	/**
	 * @param sID
	 * @param isSingle true时,表示这是单个的查询, false时会先拉取所有用户再 
	 * @return
	 */
	public static synchronized YTUser getInstance(int sID, boolean isSingle) {
		YTUser rs = null;
		
		if(!isSingle || list == null || list.size() == 0 || DateUtil.getCurrentTimestamp().after(deadline)) {
			//list = dao.findAll();
			var users = dao.findAll();
			list.clear();
			if(users != null && users.size() > 0) {
				for(YTUser u : users) {
					list.put(u.getSID(), u);
				}
				deadline = new Timestamp(DateUtil.getCurrentTimestamp().getTime() + 60000);
			}
		}
		if(list != null && !isSingle) {
			rs = list.get(sID);
		}else if(isSingle) {
			rs = dao.findById(sID);
		}
		if(rs == null) {
			rs = NULL;
		}
		return rs;
	}
	
	
	
	
	
	
	
	// xAccount xPassword xName xOption xPCName xIP xNote sStatus
	public String getXAccount() {
		return this.getStr("xAccount");
	}
	public YTUser setXAccount(String xAccount) {
		this.set("xAccount", xAccount);
		return this;
	}
	public String getXPassword() {
		return this.getStr("xPassword");
	}
	public YTUser setXPassword(String xPassword) {
		this.set("xPassword", xPassword);
		return this;
	}
	
	public String getXPCName() {
		return this.getStr("xPCName");
	}
	public YTUser setXPCName(String xPCName) {
		this.set("xPCName", xPCName);
		return this;
	}
	public String getXIP() {
		return this.getStr("xIP");
	}
	public YTUser setXIP(String xIP) {
		this.set("xIP", xIP);
		return this;
	}
	
	public int getXOption() {
		return this.getInt("xOption");
	}
	public YTUser setXOption(int xOption) {
		this.set("xOption", xOption);
		return this;
	}
	
	public String[] toArray() {  
        return new String[]{String.valueOf(this.getSID()), this.getXAccount(), this.getXPassword(), this.getXName()};  
    }  
}
